package com.library.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.management.domain.Books;
import com.library.management.dto.BookDTO;
import com.library.management.dto.BookQueryParam;
import com.library.management.mapper.BooksMapper;
import com.library.management.service.BooksService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author HY
* @description 针对表【books(图书表)】的数据库操作Service实现
* @createDate 2025-04-04 17:38:12
*/
@Service
public class BooksServiceImpl extends ServiceImpl<BooksMapper, Books>
    implements BooksService{

    @Override
    public IPage<BookDTO> getBookPage(BookQueryParam queryParam) {
        // 构建分页对象
        Page<Books> page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize());
        
        // 构建查询条件
        LambdaQueryWrapper<Books> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果提供了关键词，进行模糊查询
        if (StringUtils.hasText(queryParam.getKeyword())) {
            queryWrapper.like(Books::getTitle, queryParam.getKeyword())
                    .or()
                    .like(Books::getAuthor, queryParam.getKeyword())
                    .or()
                    .like(Books::getCategory, queryParam.getKeyword());
        }
        
        // 按创建时间倒序排序
        queryWrapper.orderByDesc(Books::getCreatedAt);
        
        // 执行查询
        IPage<Books> booksPage = page(page, queryWrapper);
        
        // 将查询结果转换为DTO
        IPage<BookDTO> bookDTOPage = booksPage.convert(this::convertToDTO);
        
        return bookDTOPage;
    }

    @Override
    public BookDTO getBookById(Integer id) {
        Books book = getById(id);
        if (book == null) {
            throw new RuntimeException("图书不存在");
        }
        return convertToDTO(book);
    }

    @Override
    public Integer addBook(BookDTO bookDTO) {
        Books book = new Books();
        BeanUtils.copyProperties(bookDTO, book);
        
        // 设置创建时间和更新时间
        Date now = new Date();
        book.setCreatedAt(now);
        book.setUpdatedAt(now);
        
        // 保存图书
        save(book);
        
        return book.getId();
    }

    @Override
    public boolean updateBook(Integer id, BookDTO bookDTO) {
        // 检查图书是否存在
        Books existingBook = getById(id);
        if (existingBook == null) {
            throw new RuntimeException("要更新的图书不存在");
        }
        
        // 复制属性
        BeanUtils.copyProperties(bookDTO, existingBook);
        
        // 设置ID和更新时间
        existingBook.setId(id);
        existingBook.setUpdatedAt(new Date());
        
        // 更新图书
        return updateById(existingBook);
    }

    @Override
    public boolean deleteBook(Integer id) {
        // 检查图书是否存在
        Books existingBook = getById(id);
        if (existingBook == null) {
            throw new RuntimeException("要删除的图书不存在");
        }
        
        // 删除图书
        return removeById(id);
    }
    
    /**
     * 将Books实体转换为BookDTO
     */
    private BookDTO convertToDTO(Books book) {
        BookDTO bookDTO = new BookDTO();
        BeanUtils.copyProperties(book, bookDTO);
        return bookDTO;
    }
    
    /**
     * 将BookDTO列表转换为Books实体列表
     */
    private List<BookDTO> convertToDTOList(List<Books> bookList) {
        return bookList.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
}




